home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / extras / mpegtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-26  |  6.8 KB  |  286 lines

  1. /*
  2.  * mpegtest.c - decode an MPEG without displaying anything; just used
  3.  * for benchmarking the MPEG Library.  Based on easympeg.c
  4.  *
  5.  * By Greg Ward, 95/05/27
  6.  */
  7.  
  8. #include <config.h>
  9.  
  10. #include <stdlib.h>
  11. #if HAVE_GETRUSAGE
  12. # include <sys/time.h>
  13. # include <sys/resource.h>
  14. #else
  15. # include <sys/types.h>
  16. # include <sys/times.h>
  17. # include <time.h>        /* for CLK_TCK */
  18. #endif
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include "ParseArgv.h"
  22. #include "mpeg.h"
  23.  
  24. int   checksum;
  25.  
  26. #if (ENABLE_DITHER)
  27. char *dither_name = NULL;
  28. char *DitherNames [] = 
  29. {
  30.    "hybrid",
  31.    "hybrid2",
  32.    "fs4",
  33.    "fs2",
  34.    "fs2fast",
  35.    "2x2",
  36.    "gray",
  37.    "fullcolor",
  38.    "none",
  39.    "ordered",
  40.    "mono",
  41.    "mono_threshold",
  42.    "ordered2",
  43.    "mbordered"
  44. };
  45. #define NUM_DITHER_TYPES 14
  46. #endif  /* ENABLE_DITHER */
  47.  
  48. /*
  49.  * Command-line options
  50.  */
  51. static ArgvInfo ArgTable [] = 
  52. {
  53. #if (ENABLE_DITHER)
  54.    { "-dither", ARGV_STRING, NULL, (char *) &dither_name, "" },
  55. #endif
  56.    { "-checksum", ARGV_CONSTANT, (char *) 1, (char *) &checksum, "" },
  57.    { NULL, ARGV_END, 0, 0, NULL }
  58. };
  59.  
  60.  
  61. void usage (char *name, char *msg)
  62. {
  63. #if (ENABLE_DITHER)
  64.    fprintf (stderr, "Usage: %s [-dither <mode>] [-checksum] mpegfile\n", name);
  65. #else
  66.    fprintf (stderr, "Usage: %s [-checksum] mpegfile\n", name);
  67. #endif
  68.    if (strlen (msg) > 0)
  69.       fprintf (stderr, "%s\n", msg);
  70.    
  71.    exit (1);
  72. }   
  73.  
  74.  
  75. /* ----------------------------- MNI Header -----------------------------------
  76. @NAME       : SearchStringTable
  77. @INPUT      : s - string to search for
  78.               Table - table of strings in which to search
  79.           TableSize - number of strings in Table[]
  80.           MinUniqueLen - the minimum length, over all strings in
  81.                              the table, required to determine uniqueness
  82.           ErrMsg - a format string for fprintf to print if s is not
  83.                    found in Table.  ErrMsg should contain two instances
  84.                of "%s"; the first will be replaced with either 
  85.                "ambiguous" or "unknown" (depending on the nature
  86.                of the error), and the second will be replaced with
  87.                the search string s.
  88. @OUTPUT     : 
  89. @RETURNS    : The position of s in Table, or -1 if not found, or -2 if
  90.               s is an ambiguous match.  Will print an error message
  91.               (constructed from ErrMsg and s) to stderr if s not found
  92.               in Table.
  93. @DESCRIPTION: Searches a list of strings for a specific string.  The
  94.               search string only has to match enough characters to be
  95.           non-ambiguous.
  96. @METHOD     : (inspired by (i.e. stolen from) ParseArgv.c)
  97. @GLOBALS    : (none)
  98. @CALLS      : 
  99. @CREATED    : 94/6/22, Greg Ward
  100. @MODIFIED   : 94/7/29, GW: added MinUniqueLen arg and changed strcmp
  101.                            to strncmp
  102.               95/3/5, GW: finally changed so that it always finds non-
  103.                           ambiguous matches without any help from caller
  104. @COMMENTS   : This should be modified so that s just has to have enough
  105.               characters to uniquely match one of the strings in Table[].
  106. ---------------------------------------------------------------------------- */
  107. int SearchStringTable (char *s, char *Table[], int TableSize, char *ErrMsg)
  108. {
  109.    int      i, match, len;
  110.  
  111.    /* Loop through the entire string table */
  112.  
  113.    match = -1;            /* indicate no match (yet) */
  114.    len = strlen (s);
  115.    
  116.    for (i = 0; i < TableSize; i++) 
  117.    {
  118.       /* Skip to next table entry if this is definitely not a match */
  119.  
  120.       if (strncmp (s, Table [i], len) != 0)
  121.      continue;
  122.  
  123.       /* If we have an *exact* match, then get outta here now */
  124.  
  125.       if (Table[i][len] == (char) 0)
  126.       {
  127.      match = i;
  128.      break;
  129.       }
  130.       
  131.       /* If we found a match in a previous iteration, then it's ambiguous */
  132.       
  133.       if (match != -1)
  134.       {
  135.      fprintf (stderr, ErrMsg, "ambiguous", s);
  136.      return (-2);
  137.       }
  138.               
  139.       /* Otherwise record the current entry as a match. */
  140.  
  141.       match = i;
  142.    }
  143.  
  144.    if (match == -1)
  145.    {
  146.       fprintf (stderr, ErrMsg, "unknown", s);
  147.    }
  148.    return (match);
  149. }     /* SearchStringTable () */
  150.  
  151.  
  152.  
  153. int Checksum (ImageDesc *img, unsigned char *pixels)
  154. {
  155.    int   i;
  156.    unsigned int   s = 0;
  157.    
  158.    for (i = 0; i < img->Size; i++)
  159.    {
  160. /*
  161.       if (i % (img->Size/16) == 0) printf ("%02x ", (unsigned int) pixels[i]);
  162.       if (i < 16) printf ("%02x ", (unsigned int) pixels[i]);
  163. */
  164.       s += (unsigned int) pixels[i];
  165.    }
  166.    return s;
  167. }   
  168.  
  169.  
  170. float current_cpu_usage ()
  171. {
  172. #if HAVE_GETRUSAGE
  173.    struct rusage  usage;
  174.  
  175.    getrusage (RUSAGE_SELF, &usage);
  176.    return (float) usage.ru_utime.tv_sec + (usage.ru_utime.tv_usec / 1e6);
  177. #else
  178.    struct tms tms;
  179.    
  180.    times (&tms);
  181.    return (float) tms.tms_utime / CLK_TCK;
  182. #endif
  183. }
  184.  
  185.  
  186. int main (int argc, char *argv[])
  187. {
  188.    FILE       *mpeg;
  189.    ImageDesc   img;
  190.    char       *pixels;
  191.    Boolean     moreframes = TRUE;
  192. #if (ENABLE_DITHER)
  193.    DitherEnum  dither_mode;
  194. #endif
  195.    int         num_frames;
  196.    float       prev_time;    /* all times are in seconds */
  197.    float       cur_time;
  198.    float       frame_elapsed;
  199.    float       total_time;
  200.  
  201.    if (ParseArgv (&argc, argv, ArgTable, ARGV_NO_DEFAULTS))
  202.    {
  203.       usage (argv[0], "");
  204.    }
  205.  
  206.    if (argc != 2) 
  207.    {
  208.       usage (argv[0], "Wrong number of arguments");
  209.    }
  210.  
  211. #if (ENABLE_DITHER)
  212.    if (dither_name != NULL)
  213.    {
  214.       dither_mode = (DitherEnum)
  215.      SearchStringTable (dither_name, DitherNames,
  216.                 NUM_DITHER_TYPES, "%s dithering type: %s\n");
  217.       if (dither_mode < 0)
  218.      usage (argv[0], "");
  219.    }
  220.    else
  221.    {
  222.       dither_mode = FULL_COLOR_DITHER;
  223.    }
  224. #endif  /* ENABLE_DITHER */
  225.    
  226.    mpeg = fopen (argv[1], "r");
  227.    if (!mpeg)
  228.    {
  229.       perror (argv[1]);
  230.       exit (1);
  231.    }
  232.       
  233. #if (ENABLE_DITHER)
  234.    SetMPEGOption (MPEG_DITHER, dither_mode);
  235. #endif
  236.    if (!OpenMPEG (mpeg, &img))
  237.    {
  238.       fprintf (stderr, "OpenMPEG on %s failed\n", argv[1]);
  239.       exit (1);
  240.    }
  241.  
  242.    pixels = (char *) malloc (img.Size * sizeof(char));
  243.    printf ("Movie is %d x %d pixels\n", img.Width, img.Height);
  244.    printf ("Required picture rate = %d, required bit rate = %d\n",
  245.        img.PictureRate, img.BitRate);
  246. #if (ENABLE_DITHER)
  247.    printf ("Requested dithering mode = %s\n", DitherNames[dither_mode]);
  248. #endif
  249.  
  250.    prev_time = current_cpu_usage ();
  251.    total_time = 0.0;
  252.    num_frames = 0;
  253.  
  254.    while (moreframes)    /* play frames until the movie ends */
  255.    {
  256.       moreframes = GetMPEGFrame (pixels);
  257.       num_frames++;
  258.       if (checksum)
  259.       {
  260.      printf ("frame %d: 0x%08x\n", 
  261.          num_frames, Checksum (&img, pixels));
  262.       }
  263.       else
  264.       {
  265.      putchar ('.'); fflush (stdout);
  266.       }
  267.      
  268.  
  269.       cur_time = current_cpu_usage ();
  270.       frame_elapsed = cur_time - prev_time;
  271.       prev_time = cur_time;
  272. /*
  273.       printf ("time for frame %d: %g sec\n",
  274.           num_frames, frame_elapsed);
  275. */
  276.       total_time += frame_elapsed;
  277.    }
  278.  
  279.    total_time /= num_frames;
  280.    printf ("\nAverage time per frame: %g sec ");
  281.    if (total_time > 0)
  282.       printf ("(%g frames/sec)\n", total_time, 1/total_time);
  283.    else
  284.       printf ("\n");
  285. }
  286.